﻿using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.GUI.Ribbon;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using System;
using System.Linq;

namespace D005_VariableParameter
{
    public class Component_VariableParameter : GH_Component, IGH_VariableParameterComponent
    {
        protected override System.Drawing.Bitmap Icon => null;
        public override Guid ComponentGuid => new Guid("1CF93B80-089A-4395-8791-BBE5BE2F8570");

        public Component_VariableParameter()
          : base("Component_VariableParameter", "加法",
              "多个数相加",
              "PluginTest", "Variable")
        {
        }

        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddIntegerParameter("加数1", "n1", "一个加数", GH_ParamAccess.item, 0);
            pManager.AddIntegerParameter("加数2", "n2", "一个加数", GH_ParamAccess.item, 0);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddIntegerParameter("和", "s", "和", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            int[] nums = new int[Params.Input.Count];

            int a = 0;

            for (int i = 0; i < Params.Input.Count; i++)
            {
                if (DA.GetData(i, ref a))
                {
                    nums[i] = a;
                }
            }

            int result = nums.Sum();
            DA.SetData(0, result);
        }

        public bool CanInsertParameter(GH_ParameterSide side, int index)
        {
            if (side == GH_ParameterSide.Output)
            {
                return false;
            }
            else if (this.Params.Input.Count >= 5)
            {
                return false;
            }
            return true;
        }

        public bool CanRemoveParameter(GH_ParameterSide side, int index)
        {
            if (side == GH_ParameterSide.Output)
            {
                return false;
            }
            else if (this.Params.Input.Count <= 2)
            {
                return false;
            }
            return true;
        }

        public IGH_Param CreateParameter(GH_ParameterSide side, int index)
        {
            Param_Integer param = new Param_Integer();

            string n = this.Params.Input.Count.ToString();
            param.NickName = "n" + n;
            param.Name = param.NickName;
            param.SetPersistentData(0);
            return param;
        }

        public bool DestroyParameter(GH_ParameterSide side, int index)
        {
            return true;
        }

        public void VariableParameterMaintenance()
        {
            int n = 1;
            foreach (var item in Params.Input)
            {
                item.NickName = "n" + n.ToString();
                item.Name = item.NickName;
                item.Optional = true;
                n++;
            }
        }
    }
}